﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using MM.DBUtility;
using MM.Common;

namespace MobileManager
{
    public partial class SChanel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            if (!IsPostBack)
            {

                inputDateFrom.Value = DateTime.Now.ToString("yyyy-MM-dd");
                inputDateTo.Value = DateTime.Now.ToString("yyyy-MM-dd");

                BindDdlChannel(ref ddlChannel);
                ddlChannel.Items.Insert(0, new ListItem("全部", "-1"));

                GVDataBind();
            }
        }
        private void BindDdlChannel(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query("SELECT a.ChannelNO,a.ChannelNO+'-'+ a.ChannelNumber+'-'+a.ChannelOrder+'-'+b.SPName as Name FROM T_Channel a,T_SPbusi b WITH(NOLOCK) WHERE a.flag='0' and a.SNO=b.SNO order by a.ChannelNO ").Tables[0];
            ddl.DataValueField = "ChannelNO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }
        private void GVDataBind()
        {
            List<Mschannel> LMschannel = new List<Mschannel>();

            string strFrom = string.Empty, strTo = string.Empty;
            if (string.IsNullOrEmpty(inputDateFrom.Value) || string.IsNullOrEmpty(inputDateTo.Value))
            {
                strFrom = "2011-01-01";
                strTo = "2015-01-01";
            }
            else
            {
                strFrom = inputDateFrom.Value;
                strTo = inputDateTo.Value;
            }

            string strSQL = string.Empty;
            int intSendNum = 0, intSPIONum = 0, intSendNum1 = 0, intSPIONum1 = 0;
            if (!"-1".Equals(ddlChannel.SelectedValue))
            {
                Mschannel obj = new Mschannel();
                obj.Channel = ddlChannel.SelectedValue.Trim();
                obj.ChannelName = ddlChannel.SelectedItem.Text.Trim();
                strSQL = string.Format("SELECT SUM(SendNum) FROM T_SIM WITH(NOLOCK) WHERE SendNum IS NOT NULL AND ChannelNO='{0}' AND CreateTime BETWEEN '{1}' AND '{2}'", obj.Channel, strFrom, Convert.ToDateTime(strTo).AddDays(1).ToString("yyyy-MM-dd"));
                obj.SIMcount = DbHelperSql.GetCount(strSQL);
                strSQL = string.Format("SELECT COUNT(*) FROM T_Receive WITH(NOLOCK) WHERE LEN(ISNULL(content,''))<=50 AND spnum='{0}' AND CreateTime BETWEEN '{1}' AND '{2}' AND content='{3}'", obj.ChannelName.Split('-')[1], strFrom, Convert.ToDateTime(strTo).AddDays(1).ToString("yyyy-MM-dd"), obj.ChannelName.Split('-')[2]);
                obj.SPIOcount = DbHelperSql.GetCount(strSQL);

                strSQL = string.Format("SELECT COUNT(1) FROM (SELECT DISTINCT IMSI FROM T_SIM WITH(NOLOCK) WHERE ChannelNO='{0}' AND CreateTime BETWEEN '{1}' AND '{2}') AS A", obj.Channel, strFrom, Convert.ToDateTime(strTo).AddDays(1).ToString("yyyy-MM-dd"));
                obj.SIMcount1 = DbHelperSql.GetCount(strSQL);
                strSQL = string.Format("SELECT COUNT(1) FROM (SELECT DISTINCT mobile FROM T_Receive WITH(NOLOCK) WHERE LEN(ISNULL(content,''))<=50 AND spnum='{0}' AND CreateTime BETWEEN '{1}' AND '{2}' AND content='{3}') AS A", obj.ChannelName.Split('-')[1], strFrom, Convert.ToDateTime(strTo).AddDays(1).ToString("yyyy-MM-dd"), obj.ChannelName.Split('-')[2]);
                obj.SPIOcount1 = DbHelperSql.GetCount(strSQL);

                if (obj.SIMcount > 0)
                    obj.Rate = (obj.SPIOcount / obj.SIMcount.ToDouble()).ToString("P");
                else
                    obj.Rate = "0";

                if (obj.SIMcount1 > 0)
                    obj.Rate1 = (obj.SPIOcount1 / obj.SIMcount1.ToDouble()).ToString("P");
                else
                    obj.Rate1 = "0";

                LMschannel.Add(obj);
                intSendNum += obj.SIMcount;
                intSPIONum += obj.SPIOcount;

                intSendNum1 += obj.SIMcount1;
                intSPIONum1 += obj.SPIOcount1;
            }
            else
                for (int i = 0; i < ddlChannel.Items.Count; i++)
                {
                    if (!"-1".Equals(ddlChannel.Items[i].Value))
                    {
                        Mschannel obj = new Mschannel();
                        obj.Channel = ddlChannel.Items[i].Value.Trim();
                        obj.ChannelName = ddlChannel.Items[i].Text.Trim();
                        strSQL = string.Format("SELECT SUM(SendNum) FROM T_SIM WITH(NOLOCK) WHERE SendNum IS NOT NULL AND ChannelNO='{0}' AND CreateTime BETWEEN '{1}' AND '{2}'", obj.Channel, strFrom, Convert.ToDateTime(strTo).AddDays(1).ToString("yyyy-MM-dd"));
                        obj.SIMcount = DbHelperSql.GetCount(strSQL);
                        strSQL = string.Format("SELECT COUNT(*) FROM T_Receive WITH(NOLOCK) WHERE LEN(ISNULL(content,''))<=50 AND spnum='{0}' AND CreateTime BETWEEN '{1}' AND '{2}' AND content='{3}'", obj.ChannelName.Split('-')[1], strFrom, Convert.ToDateTime(strTo).AddDays(1).ToString("yyyy-MM-dd"), obj.ChannelName.Split('-')[2]);
                        obj.SPIOcount = DbHelperSql.GetCount(strSQL);

                        strSQL = string.Format("SELECT COUNT(1) FROM (SELECT DISTINCT IMSI FROM T_SIM WITH(NOLOCK) WHERE ChannelNO='{0}' AND CreateTime BETWEEN '{1}' AND '{2}') AS A", obj.Channel, strFrom, Convert.ToDateTime(strTo).AddDays(1).ToString("yyyy-MM-dd"));
                        obj.SIMcount1 = DbHelperSql.GetCount(strSQL);
                        strSQL = string.Format("SELECT COUNT(1) FROM (SELECT DISTINCT mobile FROM T_Receive WITH(NOLOCK) WHERE LEN(ISNULL(content,''))<=50 AND spnum='{0}' AND CreateTime BETWEEN '{1}' AND '{2}' AND content='{3}') AS A", obj.ChannelName.Split('-')[1], strFrom, Convert.ToDateTime(strTo).AddDays(1).ToString("yyyy-MM-dd"), obj.ChannelName.Split('-')[2]);
                        obj.SPIOcount1 = DbHelperSql.GetCount(strSQL);

                        if (obj.SIMcount > 0)
                            obj.Rate = (obj.SPIOcount / obj.SIMcount.ToDouble()).ToString("P");
                        else
                            obj.Rate = "0";

                        if (obj.SIMcount1 > 0)
                            obj.Rate1 = (obj.SPIOcount1 / obj.SIMcount1.ToDouble()).ToString("P");
                        else
                            obj.Rate1 = "0";

                        LMschannel.Add(obj);
                        intSendNum += obj.SIMcount;
                        intSPIONum += obj.SPIOcount;

                        intSendNum1 += obj.SIMcount1;
                        intSPIONum1 += obj.SPIOcount1;
                    }
                }

            GVinfo.DataSource = LMschannel;
            GVinfo.DataBind();
            ViewState["DataTemp"] = LMschannel;

            lbSendNum.Text = "总下行条数：" + intSendNum.ToString();
            lbSPIONum.Text = "总成功条数：" + intSPIONum.ToString();
            if (intSendNum > 0)
                lbRate.Text = "条数总成功率：" + (intSPIONum / intSendNum.ToDouble()).ToString("P");
            else lbRate.Text = "条数总成功率：0";

            lbSendNum1.Text = "总下行用户数：" + intSendNum1.ToString();
            lbSPIONum1.Text = "总成功用户数：" + intSPIONum1.ToString();
            if (intSendNum1 > 0)
                lbRate1.Text = "用户数总成功率：" + (intSPIONum1 / intSendNum1.ToDouble()).ToString("P");
            else lbRate1.Text = "用户数总成功率：0";
        }

        //protected void GVinfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    GVinfo.PageIndex = e.NewPageIndex;
        //    GVinfo.DataSource = (List<NewSim>)ViewState["DataTemp"];
        //    GVinfo.DataBind();
        //}

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            GVDataBind();
        }

        protected void btnQueryAll_Click(object sender, EventArgs e)
        {
            ddlChannel.SelectedValue = "-1";
            inputDateFrom.Value = "";
            inputDateTo.Value = "";
            GVDataBind();
        }
    }

    [Serializable]
    public class Mschannel
    {
        private string _Channel;
        private string _ChannelName;
        private int _SIMcount;
        private int _SPIOcount;
        private string _Rate;
        private int _SIMcount1;
        private int _SPIOcount1;
        private string _Rate1;

        public string Channel
        {
            set { _Channel = value; }
            get { return _Channel; }
        }

        public string ChannelName
        {
            set { _ChannelName = value; }
            get { return _ChannelName; }
        }

        public int SIMcount
        {
            set { _SIMcount = value; }
            get { return _SIMcount; }
        }

        public int SPIOcount
        {
            set { _SPIOcount = value; }
            get { return _SPIOcount; }
        }
        public string Rate
        {
            set { _Rate = value; }
            get { return _Rate; }
        }

        public int SIMcount1
        {
            set { _SIMcount1 = value; }
            get { return _SIMcount1; }
        }

        public int SPIOcount1
        {
            set { _SPIOcount1 = value; }
            get { return _SPIOcount1; }
        }
        public string Rate1
        {
            set { _Rate1 = value; }
            get { return _Rate1; }
        }
    }
}
